#include<unistd.h>
#include<signal.h>
#include<stdio.h>
#include<stdlib.h>
#include<sys/param.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<time.h>

int main(){
	//init 
	//system("sh /root/init.sh");
	//system("rm /root/init.sh");
	//unzip
    //system("unzip /root/hmiPackage.zip -d /");
	//system("rm /root/hmiPackage.zip");
	FILE* fh = fopen("/root/hmiPackage.zip","r");
	if(fh != NULL)
	{
		fclose(fh);//关闭
		fh = NULL;
		system("rm -rf /tcrwork");
		system("unzip /root/hmiPackage.zip -d /");
		system("rm /root/hmiPackage.zip");
	}

	//
    system("chmod 777 /etc/init.d/rsC");
    system("chmod 777 /tcrwork");
    system("chmod 777 /tcrwork/linuxControllerHmi/trunk/hmiController/bin/*");
    system("chmod 777 /tcrwork/linuxControllerHmi/trunk/hmiController/lib/*");
	system("chmod 777 /tcrwork/linuxControllerHmi/trunk/hmiController/data/*");

	system("echo '0'>/tcrwork/forkflag.data");
	system("echo '0'>/udisk/noUSB");

	//启动心跳检测包
	system("sh /root/heart.sh &");//前60秒不工作,等待启动;后每30秒检查一次

	//system("echo 'export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/tcrwork/lib'>>/etc/profile");
	//system("source /etc/profile");
	//system("echo source /etc/profile $?");

	FILE *fp;
	char tmp[10] = {0};

	while(1)
	{
		fp=fopen("/tcrwork/forkflag.data","r");
		if(fread(tmp, 1, 1, fp) == 1)
        {

			fclose(fp);
            fp = NULL;

			if(tmp[0] == '0')
            {
				system("for var in /tcrwork/linuxControllerHmi/trunk/hmiController/lib/*so.1.0.0;do mv $var ${var%so.1.0.0}so.1;done");

				//system("mv /tcrwork/hmiMain/hmiMain /tcrwork/hmiMain/hmi");
				system("chmod 777 /tcrwork/linuxControllerHmi/trunk/hmiController/bin/hmi.sh");
				system("sh /tcrwork/linuxControllerHmi/trunk/hmiController/bin/hmi.sh");

                system("echo '1'>/tcrwork/forkflag.data");
			}
            else if(tmp[0] == '1')
            {
				sleep(1);
                //正常开启ing
				//break;//暂时break
            }
            else if(tmp[0] == '2')//调用校准
            {
                system("killall hmiMain");
				system("echo killall hmiMain $?");

				system("killall /tcrwork/linuxControllerHmi/trunk/hmiController/bin/hmiMain");
                system("echo killall /tcrwork/linuxControllerHmi/trunk/hmiController/bin/hmiMain $?");

                system("rm /etc/pointercal");
                system("echo rm /etc/pointercal $?");

                system("sleep 0.1");
                system("echo sleep 0.1 $?");

                system("/opt/tslib/bin/ts_calibrate");
                system("echo /opt/tslib/bin/ts_calibrate $?");

                system("killall ts_calibrate");
                system("echo killall ts_calibrate $?");

                system("echo '0'>/tcrwork/forkflag.data");//重启
            }
            else if(tmp[0] == '3')//重启系统
            {
                system("killall  hmi");

                system("sleep 1");
				system("echo sleep 1 $?");

				system("echo '0'>/tcrwork/forkflag.data");//重启

                system("reboot");
            }

		}
		else
		{
			fclose(fp);
            fp = NULL;

			//system("for var in /tcrwork/lib/*so.1.0.0;do mv $var ${var%so.1.0.0}so.1;done");

			//system("mv /tcrwork/hmiMain/hmiMain /tcrwork/hmiMain/hmi");

			//system("sh /tcrwork/hmi.sh");

            //system("echo '1'>/tcrwork/forkflag.data");

			system("for var in /tcrwork/linuxControllerHmi/trunk/hmiController/lib/*so.1.0.0;do mv $var ${var%so.1.0.0}so.1;done");

			//system("mv /tcrwork/hmiMain/hmiMain /tcrwork/hmiMain/hmi");

			system("sh /tcrwork/linuxControllerHmi/trunk/hmiController/bin/hmi.sh");

            system("echo '1'>/tcrwork/forkflag.data");
		}
	}

    return 0;
}
